package com.yifan.servlet;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.yifan.entity.Software;
import com.yifan.service.SoftwareService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

@WebServlet("/software")
public class SoftwareServlet extends HttpServlet{
	
	SoftwareService softwareService = new SoftwareService();

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {	
		doPost(request, response);
	}
	
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("utf-8");
		//获取请求的方法
		String method = request.getParameter("method");
		//请求分发
		if("getSoftwares".equals(method)){
			getSoftwares(request, response);
		}else if("getSoftwares2".equals(method)){
			getSoftwares2(request, response);
		}else if("getSoftware".equals(method)) {
			getSoftware(request, response);
		}else if("updateSoftware".equals(method)) {
			updateSoftware(request, response);
		}else if("addSoftware".equals(method)) {
			addSoftware(request, response);
		}else if("deleteSoftware".equals(method)) {
			deleteSoftware(request, response);
		}else if("deleteSoftwares".equals(method)) {
			deleteSoftwares(request, response);
		}

	}

	private void getSoftwares(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
		response.setCharacterEncoding("utf-8");
		String softwareName = request.getParameter("softwareName");
		//获取分页参数
		int page = 1;//Integer.parseInt(request.getParameter("pageIndex"));
		int pageSize = 76;//Integer.parseInt(request.getParameter("pageSize"));
		
		Map<String, Object> softwares = softwareService.getSoftwares(softwareName, page, pageSize);
		// response.getWriter().write(JSON.toJSONStringWithDateFormat(softwares,"yyyy-MM-dd HH:mm:ss",SerializerFeature.WriteDateUseDateFormat));
		request.setAttribute("softwares", softwares.get("data"));
		request.getRequestDispatcher("/software.jsp").forward(request,response);
	}

	private void getSoftwares2(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
		response.setCharacterEncoding("utf-8");
		String softwareName = request.getParameter("softwareName");
		//获取分页参数
		int page = 1;//Integer.parseInt(request.getParameter("pageIndex"));
		int pageSize = 76;//Integer.parseInt(request.getParameter("pageSize"));

		Map<String, Object> softwares = softwareService.getSoftwares(softwareName, page, pageSize);
		// response.getWriter().write(JSON.toJSONStringWithDateFormat(softwares,"yyyy-MM-dd HH:mm:ss",SerializerFeature.WriteDateUseDateFormat));
		request.setAttribute("softwares", softwares.get("data"));
		request.getRequestDispatcher("/software2.jsp").forward(request,response);
	}

	private void getSoftware(HttpServletRequest request, HttpServletResponse response) throws IOException {
		int id = Integer.parseInt(request.getParameter("id"));
		Map<String, Object> software = softwareService.getSoftware(id);
		System.out.println(JSON.toJSON(software));
		response.getWriter().write(JSON.toJSONStringWithDateFormat(software,"yyyy-MM-dd HH:mm:ss",SerializerFeature.WriteDateUseDateFormat));
	}
	
	private void updateSoftware(HttpServletRequest request, HttpServletResponse response) throws IOException {
		int id = Integer.parseInt(request.getParameter("id"));
		String name = request.getParameter("name");
		String company = request.getParameter("company");
		String type = request.getParameter("type");
		String price = request.getParameter("price");
		String size = request.getParameter("size");
		String createDate = request.getParameter("createDate");
		Software software = new Software(id,name, company, type, price, size, createDate);
		Map<String, String> map = softwareService.updateSoftware(software);
		response.getWriter().write(JSON.toJSON(map).toString());
	}
	
	private void deleteSoftware(HttpServletRequest request, HttpServletResponse response) throws IOException {
		int id = Integer.parseInt(request.getParameter("id"));
		
		Map<String, String> map = softwareService.deleteSoftware(id);
		response.getWriter().write(JSON.toJSON(map).toString());
	}
	
	private void deleteSoftwares(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String ids = request.getParameter("ids");
		String[] idarr = ids.substring(0, ids.length()-1).split(",");
		System.out.println(idarr.toString());
		Map<String, String> map = softwareService.deleteSoftwares(idarr);
		response.getWriter().write(JSON.toJSON(map).toString());
	}
	
	private void addSoftware(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String name = request.getParameter("name");
		String company = request.getParameter("company");
		String type = request.getParameter("type");
		String price = request.getParameter("price");
		String size = request.getParameter("size");
		String createDate = request.getParameter("createDate");
		Software software = new Software(0,name, company, type, price, size, createDate);
		Map<String, String> map = softwareService.addSoftware(software);
		response.getWriter().write(JSON.toJSON(map).toString());
	}
}
